using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;

namespace FileDownloadServer.Controllers
{
	class FileDownloadResponseBuilder
	{
		private readonly FileDownloadProperties download;
		private readonly HttpResponseMessage response;

		public FileDownloadResponseBuilder(FileDownloadProperties download)
		{
			this.download = download;
			this.response = new HttpResponseMessage();
		}

		public HttpResponseMessage Build()
		{
			response.Content = new ByteArrayContent(download.File);

			AddIfNotNull("Content-Type", ResponseHeaders.ContentType);
			AddIfNotNull("Content-Disposition", ResponseHeaders.ContentDisposition);

			return response;
		}

		private void AddIfNotNull(string header, string value)
		{
			if (value != null)
				response.Content.Headers.Add(header, value);
		}

		private ResponseHeaders ResponseHeaders
		{
			get
			{
				return download.ResponseHeaders;
			}
		}
	}
}
